Sending a HTTP POST using requests

Overview:

  • In HTTP, the method POST is used for sending data to a HTTP server from a HTTP client (e.g. A Web Browser), typically for the purpose of storing such data or for making decisions about the next page at the server side.
  • It is not uncommon now a days for a web server be it a primary web server or a secondary web server to host multiple websites.
  • In any case, a HTTP POST to a multi-tenanted webserver or a single-tenanted webserver means the data is intended for storage by the business applications of the web sites. The HTTP server merely executes the HTTP protocol for any URL available on the web server.
  • Example use cases include,

a) Sign-up for a service like GMail, Flickr 

b) Signing up of a visitor information page on a web site

  • Remember that, for queries to search for an occurrence of a string over a set of web pages the HTTP GET method can be used.
  • The GET method is an idempotent action on the server. Upon receiving of a request for a HTTP GET nothing can be changed in the web server except the retrieval log maintained at the web server.
  • The method POST of the requests module can be used to send some data from a HTTP client to a HTTP server.

 

Example:

import requests

 

postURL  = "https://httpbin.org/post"

 

# Send the HTTP POST request

response = requests.post(postURL, data = {'key':'value'})

 

# Print the response for the HTTP POST made

print("HTTP response code is:%d"%response.status_code)

 

# Print the HTTP header

print("HTTP header:%s"%response.headers)

 

Output:

HTTP response code is:200

HTTP header:{'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Date': 'Wed, 30 Jan 2019 11:19:41 GMT', 'Content-Type': 'application/json', 'Content-Length': '440', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}

 


Copyright 2024 © pythontic.com